//
//  PPcommentDisplayView.m
//  PetParadise
//
//  Created by 陈无忌 on 8/4/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPcommentDisplayView.h"
#import "PPComment.h"

@implementation PPcommentDisplayView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

+(CGFloat) getViewHeigt:(NSArray <PPComment,Optional>*)commentArray
{
    CGFloat height = 8;
    NSMutableArray *heightArray = [[NSMutableArray alloc] init];
    int count = commentArray.count;
    if (count == 0) {
        return 0;
    }else{
        for (int i = 0; i < count; i++) {
            PPComment *mycomment = [commentArray objectAtIndex:i];
            PPLabel *commentLb = [[PPLabel alloc] initWithFrame:CGRectMake(10, 8, 220, 12)];
            commentLb.font = [UIFont fontWithName:nil size:12];
            if (mycomment.targetUserId == 0) {
                commentLb.text = [NSString stringWithFormat:@"%@:%@",mycomment.userName,mycomment.content];
            }else{
                commentLb.text = [NSString stringWithFormat:@"%@ 回复 %@:%@",mycomment.userName,mycomment.targetUserName,mycomment.content];
            }
            commentLb.numberOfLines = 0;
            commentLb.lineBreakMode = NSLineBreakByCharWrapping;
            [commentLb sizeToFit];
            [heightArray addObject:[NSNumber numberWithFloat:commentLb.frame.size.height]];
        }
        for (int i = 0; i < count; i++) {
            NSNumber *labelHeight = [heightArray objectAtIndex:i];
            height = height + labelHeight.floatValue + 8;
        }
        return height;
    }
}
+(PPcommentDisplayView *)setViewWithComment:(NSArray <PPComment,Optional>*)comment andView:(PPcommentDisplayView *)view
{
    for (PPLabel *subView in view.subviews)
    {
        [subView removeFromSuperview];
    }
    int count = comment.count;
    NSMutableArray *heightArray = [[NSMutableArray alloc] init];
    if (count == 0) {
        view.hidden = YES;
        return view;
    }
    view.hidden = NO;
    for (int i = 0; i < count; i++) {
        PPComment *mycomment = [comment objectAtIndex:i];
        PPLabel *commentLb = [[PPLabel alloc] initWithFrame:CGRectMake(10, 8, 220, 12)];
        if (mycomment.targetUserId == 0) {
            commentLb.text = [NSString stringWithFormat:@"%@:%@",mycomment.userName,mycomment.content];
        }else{
            commentLb.text = [NSString stringWithFormat:@"%@ 回复 %@:%@",mycomment.userName,mycomment.targetUserName,mycomment.content];
        }
        commentLb.font = [UIFont fontWithName:@"Arial" size:12];
        
        commentLb.numberOfLines = 0;
        commentLb.lineBreakMode = NSLineBreakByCharWrapping;
        [commentLb sizeToFit];
        [heightArray addObject:[NSNumber numberWithFloat:commentLb.frame.size.height]];
        CGFloat height = 0;
        for (int j = 0; j < heightArray.count; j++) {
            NSNumber *hlb = [heightArray objectAtIndex:j];
            height = height + hlb.floatValue;
        }
        
        NSNumber *nowLbHeight = [heightArray objectAtIndex:(heightArray.count-1)];
        commentLb.frame = CGRectMake(10, 8*(i+1) + height - nowLbHeight.floatValue, 220, commentLb.frame.size.height);
        [view addSubview:commentLb];
    }
    
    return view; 
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
